<?php

/**
 * Log Model
 *
 */
class Application_Model_Log
{
    const DB_TABLE='log';
    const RESOURCE_NAME='log';
    
    protected $_data;
    
    public function __construct($log=null)
    {
        if (!empty($log) && (is_array($log) || (is_object($log) && ($log instanceof Zend_Db_Table_Row))))
        {
            $this->_init($log);
        } else 
        {
            $this->_init(array(
                'id' => 0,
                'ip' => '',
                'ua' => '',
                'referer' => '',
                'url' => '',
                'tstamp' => 0
            ));
        } 
    }
    
    /**
     * Magic function
     *
     * @param string $var
     */
    public function __get($var)
    {
        return $this->_getData($var);
    }
    
    /**
     * Magic function
     *
     * @param string $var
     * @param mixed $val
     */
    public function __set($var,$val)
    {
        $this->_setData($var,$val);
    }
    
    /**
     * Magic function
     *
     * @param string $var
     */
    public function __unset($var)
    {
        if (array_key_exists($var,$this->_data))
        {
            unset($this->_data[$var]);
        }
    }
    
    /**
     * Magic function
     *
     * @param string $var
     * @return bool
     */
    public function __isset($var)
    {
        return array_key_exists($var,$this->_data);
    }
    
    /**
     * Magic function
     *
     * @return string
     */
    public function __toString()
    {
        return (string) $this->ip;
    }
    
    /**
     * Initializes node
     *
     * @param mixed $user
     */
    protected function _init($log)
    {
        foreach ($log as $var=>$val)
        {
            $this->_setData($var,$val);
        }
    }
    
    /**
     * Gets variable value
     *
     * @param string $var
     * @return mixed
     */
    protected function _getData($var)
    {
        $method='_getValue'.ucwords($var);
        
        if (method_exists($this,$method))
        {
            return $this->{$method}();
        }
        
        return null;
    }
    
    /**
     * Sets variable value
     *
     * @param string $var
     * @param mixed $val
     */
    protected function _setData($var,$val)
    {
        $method='_setValue'.ucwords($var);
        
        if (method_exists($this,$method))
        {
            $this->{$method}($val);
        }
    }

    /**
     * Returns log ID
     *
     * @return int
     */
    protected function _getValueId()
    {
        return (int) $this->_data['id'];
    }
    
    /**
     * Returns log IP
     *
     * @return string
     */
    protected function _getValueIp()
    {
        return (string) $this->_data['ip'];
    }
    
    /**
     * Returns log User Agent
     *
     * @return string
     */
    protected function _getValueUa()
    {
        return (string) $this->_data['ua'];
    }
    
    /**
     * Returns log referer
     *
     * @return string
     */
    protected function _getValueReferer()
    {
        return (string) $this->_data['referer'];
    }
    
    /**
     * Returns log URL
     *
     * @return string
     */
    protected function _getValueUrl()
    {
        return (string) $this->_data['url'];
    }
    
    /**
     * Returns log time stamp
     *
     * @return int
     */
    protected function _getValueTstamp()
    {
        return (int) $this->_data['tstamp'];
    }

     /* Sets log ID
     *
     * @param int $val
     */
    protected function _setValueId($val)
    {
        $this->_data['id']=(int) $val;
    }
    
     /* Sets log IP
     *
     * @param string $val
     */
    protected function _setValueIp($val)
    {
        $this->_data['ip']=(string) $val;
    }
    
    /* Sets log User Agent
     *
     * @param string $val
     */
    protected function _setValueUa($val)
    {
        $this->_data['ua']=(string) $val;
    }
    
    /* Sets log referer
     *
     * @param string $val
     */
    protected function _setValueReferer($val)
    {
        $this->_data['referer']=(string) $val;
    }
    
    /* Sets log URL
     *
     * @param string $val
     */
    protected function _setValueUrl($val)
    {
        $this->_data['url']=(string) $val;
    }
    
    /* Sets log time stamp
     *
     * @param int $val
     */
    protected function _setValueTstamp($val)
    {
        $this->_data['tstamp']=(int) $val;
    }

     /**
     * Returns an array with log data
     *
     * @return array
     */
    public function toArray()
    {
        return $this->_data;
    }
}

